const connection = require("../app/database");

// 数据库操作抽离
class UserService {
  async create(user) {
    // 将user对象保存到数据库
    const { name, password } = user;

    //  拼接sql语句
    const statement = "INSERT INTO `users`(name,password) VALUES(? , ?)";
    // 异步操作
    const [result] = await connection.execute(statement, [name, password]);

    return result;
  }

  async findUser(name) {
    const statement = "SELECT * FROM `users` WHERE `name` = ?";
    const [values] = await connection.execute(statement, [name]);

    return values;
  }
}

module.exports = new UserService();
